package com.le.buddha;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nls.client.AccessToken;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;

/**
 * @author guole
 * @created 2024/10/2
 */
public class CosyVoiceDemo {
    //域名
    private static final String DOMAIN = "nls-slp.cn-shanghai.aliyuncs.com";
    // API版本
    private static final String API_VERSION = "2019-08-19";

    private static final IAcsClient client;

    static {
        // 创建DefaultAcsClient实例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai","LTAI5t6rV2sRnzL5p1WpAQse"
              ,
                "lqns8N8N7C4FqGYniJbC5ERHrIM0hx");
        client = new DefaultAcsClient(profile);
    }

    public static void main(String[] args) throws InterruptedException {
        //克隆后名称
        String voicePrefix = "yangmi2";
        //音频地址
        String url = "http://49.234.50.111/images/2.mp3";
         cosyClone(voicePrefix, url);
   //   cosyList(voicePrefix);
    //   byte[] bytes= textToSpeech();
    //   FileUtil.writeBytes(bytes,"d:/22.mp3");
     //   System.out.println(bytes);
    }

    private static void cosyList(String voicePrefix) {
        CommonRequest request = buildRequest("ListCosyVoice");
        request.putBodyParameter("VoicePrefix", voicePrefix);
        String response = sendRequest(request);
        System.out.println(response);
    }

    private static void cosyClone(String voicePrefix, String url) {
        CommonRequest cloneRequest = buildRequest("CosyVoiceClone");
        cloneRequest.putBodyParameter("VoicePrefix", voicePrefix);
        cloneRequest.putBodyParameter("Url", url);
        // 设定等待超时时间为15s
        cloneRequest.setSysReadTimeout(15000);
        long startTime = System.currentTimeMillis();
        String response = sendRequest(cloneRequest);
        long endTime = System.currentTimeMillis();
        System.out.println(response);
        System.out.println("cost: "+ (endTime - startTime) + " 毫秒");
    }

    private static CommonRequest buildRequest(String popApiName) {
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(API_VERSION);
        request.setAction(popApiName);
        request.setProtocol(ProtocolType.HTTPS);
        return request;
    }

    private static String sendRequest(CommonRequest request) {
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

     private static byte[] textToSpeech()  {
         try {
             AccessToken accessToken = new AccessToken("LTAI5t6rV2sRnzL5p1WpAQse", "lqns8N8N7C4FqGYniJbC5ERHrIM0hx");
             accessToken.apply();
             String token = accessToken.getToken();
             //文本消息转语音
             String url = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts";
             HttpRequest request = HttpUtil.createPost(url);
             HashMap<String, String> headers = new HashMap<>();
             headers.put("Content-Type", "application/json");
             request.addHeaders(headers);
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("appkey", "sMvM7FonodPcw708");
             jsonObject.put("text", "我是一个傻子");
             jsonObject.put("token", token);
             jsonObject.put("format", "mp3");
             jsonObject.put("voice", "cosyvoice-yangmi-f528d00");
             jsonObject.put("speech_rate", 0);
             request.body(jsonObject.toJSONString());

             HttpResponse httpResponse = request.execute();
          return    httpResponse.bodyBytes();
         } catch (Exception e) {
              e.printStackTrace();
         }
         return null;
    }
}
